//
//  SISSChatRecommendCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/27.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSChatRecommendCell.h"
#import "EaseBubbleView+ChatRecommend.h"

static const CGFloat kCellHeight = 140.0f;
@implementation SISSChatRecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier model:(id<IMessageModel>)model{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier model:model];
    if (self) {
        self.avatarSize = 40.f;
        self.avatarCornerRadius = 20.f;
        self.hasRead.hidden = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (BOOL)isCustomBubbleView:(id)model{
    return YES;
}

- (void)setCustomModel:(id<IMessageModel>)model{
    UIImage *image = model.image;
    if (!image) {
        [self.bubbleView.imageView sd_setImageWithURL:[NSURL URLWithString:model.fileURLPath] placeholderImage:[UIImage imageNamed:model.failImageName]];
    } else {
        _bubbleView.imageView.image = image;
    }
    
    if (model.avatarURLPath) {
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatarURLPath] placeholderImage:model.avatarImage];
    } else {
        self.avatarView.image = model.avatarImage;
    }
}

- (void)setCustomBubbleView:(id)model{
    [_bubbleView setupChatRecommandBubbleView];
    
    _bubbleView.imageView.image = [UIImage imageNamed:@"头像"];
}

- (void)updateCustomBubbleViewMargin:(UIEdgeInsets)bubbleMargin model:(id<IMessageModel>)mode{
    [_bubbleView updateChatRecommandMargin:bubbleMargin];
    _bubbleView.translatesAutoresizingMaskIntoConstraints = YES;
    
    CGFloat bubbleViewHeight = 100;// 气泡背景图高度
    CGFloat nameLabelHeight = 15;// 昵称label的高度
    
    if (mode.isSender) {
        _bubbleView.frame = CGRectMake(SCREEN_WIDTH - 300.5, nameLabelHeight, 250, bubbleViewHeight);
    }else{
        _bubbleView.frame = CGRectMake(55, nameLabelHeight, 250, bubbleViewHeight);
        
    }
    // 这里强制调用内部私有方法
    //    [_bubbleView _setupConstraintsXX];
    
}


+ (NSString *)cellIdentifierWithModel:(id<IMessageModel>)model {
    return NSStringFromClass([self class]);
}

+ (CGFloat)cellHeightWithModel:(id<IMessageModel>)model {
    return kCellHeight;
}

- (void)setModel:(id<IMessageModel>)model {
    [super setModel:model];
    
    NSDictionary *dict = model.message.ext;
    
    NSString *str;
    NSString *titleStr;
    NSString *contentStr;
    if ([dict[@"recommendType"] integerValue] == 1) { // 房源推荐
        str = @"推荐房源";
        titleStr = [NSString stringWithFormat:@"%@ | %@万",dict[@"neighbourhood"],dict[@"price"]];
        contentStr = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",dict[@"region"],dict[@"regionDetail"],dict[@"area"],dict[@"houseType"]];
        
    } else if ([dict[@"recommendType"] integerValue] == 2) { // 客源推荐
        str = @"推荐客源";
        titleStr = [NSString stringWithFormat:@"%@求购",dict[@"name"]];
        contentStr = [NSString stringWithFormat:@"%@-%@万 | %@-%@ | %@",dict[@"minPrice"],dict[@"maxPrice"],dict[@"region"],dict[@"regionDetail"],dict[@"houseType"]];
    } else if ([dict[@"recommendType"] integerValue] == 4) { // 推荐出租
        str = @"推荐出租";
        titleStr = [NSString stringWithFormat:@"%@ | %@元",dict[@"neighbourhood"],dict[@"monthlyRent"]];
        contentStr = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",dict[@"region"],dict[@"regionDetail"],dict[@"area"],dict[@"houseType"]];
        
    } else if ([dict[@"recommendType"] integerValue] == 5) { // 推荐租客
        str = @"推荐租客";
        titleStr = [NSString stringWithFormat:@"%@求租",dict[@"renterName"]];
        contentStr = [NSString stringWithFormat:@"%@-%@元 | %@-%@ | %@",dict[@"minPrice"],dict[@"maxPrice"],dict[@"region"],dict[@"regionDetail"],dict[@"houseType"]];
    }
    UIColor *textColor = self.model.isSender ? COLOR(0xFFFFFF) : COLOR(0x334A60);
    self.bubbleView.recommendTitleLabel.textColor = textColor;
    self.bubbleView.firstTextLabel.textColor = textColor;
    self.bubbleView.secondTextLabel.textColor = textColor;
    
    self.bubbleView.recommendTitleLabel.text = str;
    self.bubbleView.firstTextLabel.text = titleStr;
    self.bubbleView.secondTextLabel.text = contentStr;
    _hasRead.hidden = YES;//名片消息不显示已读
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    NSString *imageName = self.model.isSender ? @"" : @"";
//    UIImage *image = self.model.isSender ? [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:30 topCapHeight:35] :
//    [[UIImage imageNamed:imageName] stretchableImageWithLeftCapWidth:20 topCapHeight:35];
    // 等待接入名片的背景图片
//    self.bubbleView.backgroundImageView.image = image;
}

@end
